set more off
clear all
set matsize 11000
set maxvar 120000
set linesize 255

use $glData/data_survey, clear

cd $glOut

* list of controls
global controls "i.age_cat male college i.fam_income sup1 d1 color1 ideo1"

eststo clear
eststo est1: quietly reg voted d1Xp1 color1Xp1 ideo1Xp1 $controls sido_cand_dum* if polk_level == 1
eststo est2: quietly reg voted d1Xp1 color1Xp1 ideo1Xp1 $controls sido_cand_dum* if polk_level == 2
quietly suest est1 est2, cluster(w4_id)

quietly test [est1_mean]d1Xp1 = [est2_mean]d1Xp1
local pval11 = r(p)
quietly test [est1_mean]color1Xp1 = [est2_mean]color1Xp1
local pval12 = r(p)
quietly test [est1_mean]ideo1Xp1 = [est2_mean]ideo1Xp1
local pval13 = r(p)

eststo clear
eststo est1: quietly reg voted d1Xp1 color1Xp1 ideo1Xp1 $controls sido_cand_dum* if elec_int == 0
eststo est2: quietly reg voted d1Xp1 color1Xp1 ideo1Xp1 $controls sido_cand_dum* if elec_int == 1
quietly suest est1 est2, cluster(w4_id)

quietly test [est1_mean]d1Xp1 = [est2_mean]d1Xp1
local pval21 = r(p)
quietly test [est1_mean]color1Xp1 = [est2_mean]color1Xp1
local pval22 = r(p)
quietly test [est1_mean]ideo1Xp1 = [est2_mean]ideo1Xp1
local pval23 = r(p)

eststo clear
eststo est1: quietly areg voted d1Xp1 color1Xp1 ideo1Xp1 $controls if polk_level == 1, a(sido_cand) cluster(w4_id)
eststo est2: quietly areg voted d1Xp1 color1Xp1 ideo1Xp1 $controls if polk_level == 2, a(sido_cand) cluster(w4_id)
eststo est3: quietly areg voted d1Xp1 color1Xp1 ideo1Xp1 $controls if elec_int == 0, a(sido_cand) cluster(w4_id)
eststo est4: quietly areg voted d1Xp1 color1Xp1 ideo1Xp1 $controls if elec_int == 1, a(sido_cand) cluster(w4_id)


local table_title = "Political Knowledge, Political Interest, and the Relative Saliency of Cues: Micro-Level Analysis"
local table_name  = "table4"

esttab using "`table_name'_tmp.tex", ///
	replace label nomtitles fragment booktabs nonumbers nolines ///
	star(* 0.1 ** 0.05 *** 0.01) nodepvar nonote b(%5.3f) se(%5.3f) noobs ///
	keep  (d1 d1Xp1 color1Xp1 ideo1Xp1) ///
	order (d1 d1Xp1 color1Xp1 ideo1Xp1) ///
	substitute("XX" "$\times$ \\ \hspace{6pt}" "beta\_" "beta_") 

quietly {
	capture log close
  
	log using "`table_name'.tex", text replace

	noisily display "\begin{table}[htbp]"
	noisily display "\centering"
    noisily display "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}"	
	noisily display "\begin{threeparttable}"
	noisily display "\caption{`table_title'}"
	noisily display "\label{`table_name'}"
	noisily display "\begin{tabular}{lcccc}"
	noisily display "\midrule\midrule"
	noisily display "&\multicolumn{4}{c}{Outcome: Voted for the Candidate}\\"
	noisily display "\cmidrule(r){2-5}"
	noisily display "&\multicolumn{2}{c}{Political Knowledge} &\multicolumn{2}{c}{Interest in Election}\\"
	noisily display "\cmidrule(r){2-3}\cmidrule(r){4-5}\cmidrule(r){4-4}"
	noisily display "&\multicolumn{1}{c}{Low} &\multicolumn{1}{c}{High}&\multicolumn{1}{c}{No} &\multicolumn{1}{c}{Yes}\\"
	noisily display "\cmidrule(r){2-2}\cmidrule(r){3-3}\cmidrule(r){4-4}\cmidrule(r){5-5}" 
	noisily display "&\multicolumn{1}{c}{(1)} &\multicolumn{1}{c}{(2)}&\multicolumn{1}{c}{(3)} &\multicolumn{1}{c}{(4)}\\"
	noisily display "\midrule"
	noisily display "\addlinespace"
	noisily display "\expandableinput `table_name'_tmp.tex"
	noisily display "\midrule"	
	noisily display _asis " $ p$-value of equality test: & $\beta_5^{Low} = \beta_5^{High}$ & " %5.3f `pval11' "& $\beta_5^{No} = \beta_5^{Yes}$ & " %5.3f `pval21' "\\"
	noisily display "\addlinespace"			
	noisily display _asis " & $\beta_6^{Low} = \beta_6^{High}$ & " %5.3f `pval12' "& $\beta_6^{No} = \beta_6^{Yes}$ & " %5.3f `pval22' "\\"
	noisily display "\addlinespace"			
	noisily display _asis " & $\beta_7^{Low} = \beta_7^{High}$ & " %5.3f `pval13' "& $\beta_7^{No} = \beta_7^{Yes}$ & " %5.3f `pval23' "\\"
	noisily display "\midrule"
	noisily display "\addlinespace"			
	noisily display "Obs."
	forvalues i = 1/4 {
		estimates restore est`i'
		local obs = e(N)
		noisily display "&" %6.0fc `obs'
	}
	
	noisily display "\\"	
	noisily display "\midrule\midrule"
	noisily display "\end{tabular}"
	noisily display "\begin{tablenotes}"
	noisily display "\footnotesize"
	noisily display "\item \textit{Notes:}"
	noisily display "Robust standard errors, clustered at the respondent level, are in parentheses."
	noisily display "Province $\times$ candidate fixed effects and "
	noisily display "Control variables are included in all columns."
	noisily display "\item *** \$p<0.01$; ** \$p<0.05\$; * \$p<0.1\$."
	noisily display "\end{tablenotes}"
	noisily display "\end{threeparttable}"
	noisily display "\end{table}"
  
	log off
  
	log close
}
